<template>
  <div class="addMenus">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="菜单名称">
        <el-input class="input" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单">
        <el-select v-model="form.pid" placeholder="请选择上级菜单">
          <el-option v-for="item in list" :key="item.id" :label="item.title" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单类型">
        <el-radio-group v-model="form.type">
          <el-radio label="1">目录</el-radio>
          <el-radio label="2">菜单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="input" label="菜单图标" v-show="form.type == 1">
        <el-input v-model="form.icon"></el-input>
      </el-form-item>
      <el-form-item class="input" label="菜单地址" v-show="form.type == 2">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      
      <el-switch
        class="mode"
        v-model="form.status"
        inactive-text="状态"
        :active-value="1" :inactive-value="2"
      >
      </el-switch>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
        <el-button type="info" @click="back">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addMenuApi,getOneApi } from '@/api/menu'
export default {
  data() {
    return {
        flag: false,
        list:[],
      form: {
        title: "",
        pid: "",
        type: "1",
        icon: "",
        status: 1,
        url: '', 
      },
    };
  },
  mounted(){
    this.getList()
  },
  methods: {
    //提交添加事件
    async onSubmit() {
      let res=await addMenuApi(this.form)
      if(res.code==200){
        alert(res.msg)
        this.clearFormData()
        this.$Bus.$emit('updata')
      }else{
        alert(res.msg)
      }
    },
    async getList() {
      let res=await getOneApi(true)
      if(res.code==200){
        this.list=res.list
       /*  console.log(11111,this.list); */
      }else{
        alert(res.msg)
      }
    },
    //清除表单
    clearFormData () {
      this.form = {
        pid: '',
        title: '',
        icon: '',
        type: 1,
        url: '',
        status: 1
      }
    },
    //刷新侧边栏
    back(){
        this.$router.push('/menu')
    }
  },
};
</script>
<style scoped lang="less">
.addMenus {
  .input {
    width: 40%;
  }
  .mode{
    margin: 0 0 30px 12px;
  }
}
</style>
